//
//  UITableView+RWAdditions.m
//  Alumna
//
//  Created by ray on 2021/11/16.
//

#import "UITableView+RWAdditions.h"
#import "UITableViewCell+RWAdditions.h"
#import "UITableViewHeaderFooterView+RWAdditions.h"

@implementation UITableView (RWAdditions)

- (void)rw_registerClass:(nullable Class)cellClass {
    [self registerClass:cellClass forCellReuseIdentifier:NSStringFromClass(cellClass)];
}

- (void)rw_registerNibClass:(nullable Class)cellClass {
    [self registerNib:[UINib nibWithNibName:NSStringFromClass(cellClass) bundle:nil] forCellReuseIdentifier:NSStringFromClass(cellClass)];
}

- (void)rw_registerHeaderFooterViewClass:(nullable Class)cellClass {
    [self registerClass:cellClass forHeaderFooterViewReuseIdentifier:NSStringFromClass(cellClass)];
}

- (void)rw_registerHeaderFooterViewNibClass:(nullable Class)cellClass {
    [self registerNib:[UINib nibWithNibName:NSStringFromClass(cellClass) bundle:nil] forHeaderFooterViewReuseIdentifier:NSStringFromClass(cellClass)];
}

- (__kindof UITableViewCell *)rw_dequeueReusableCellWithClass:(nullable Class)cellClass {
    UITableViewCell *cell = [self dequeueReusableCellWithIdentifier:NSStringFromClass(cellClass)];
    [self p_superTableViewWithRWCell:cell];
    return cell;
}

- (__kindof UITableViewCell *)rw_dequeueReusableCellWithClass:(nullable Class)cellClass
                                                 forIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [self dequeueReusableCellWithIdentifier:NSStringFromClass(cellClass) forIndexPath:indexPath];
    [self p_superTableViewWithRWCell:cell];
    
    if (indexPath && [cell respondsToSelector:NSSelectorFromString(@"setIndexPath:")]) {
        [cell setValue:indexPath forKey:@"indexPath"];
    }
    return cell;
}

- (__kindof UITableViewCell *)rw_loadNibCellClss:(nullable Class)cellClass owner:(nullable id)owner {
    UITableViewCell *cell = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(cellClass) owner:self options:nil].lastObject;
    [self p_superTableViewWithRWCell:cell];
    return cell;
}

- (nullable __kindof UITableViewHeaderFooterView *)rw_dequeueReusableHeaderFooterViewWithClass:(nullable Class)cellClass {
    UITableViewHeaderFooterView *view = [self dequeueReusableHeaderFooterViewWithIdentifier:NSStringFromClass(cellClass)];
    [self p_superTableViewWithRWHeaderFooterView:view];
    return view;
}

/// 赋值tableView
- (void)p_superTableViewWithRWCell:(UITableViewCell *)cell {
    cell.superTableView = self;
}

/// 赋值tableView
- (void)p_superTableViewWithRWHeaderFooterView:(UITableViewHeaderFooterView *)view {
    view.superTableView = self;
}

@end
